// pages/distribute/goods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageUrlFix: app.d.imageUrlFix,
    typesList: [],//商品分类列表
    currType: 0,//当前类型
    goodsList: [],
    shopId: -1,//店铺Id
    pageNum: 1,//页面
    status: 1,//1刷新， 2加载
    isNoGoods: false,
    loadLength: 5,
  },

  /**
   * 查询左侧商品分类列表
   */
  queryTypesList: function(){
    var that = this;
    app.request("WxGoodsController", "queryGoodsShopList", {ru_id: app.d.userId}, function (data) {
      var list = data.list;
      const currId = list[0].id;
      const shopId = data.shopInfo[0].id;
      that.setData({
        typesList: list,
        currType: currId,
        shopId: shopId,
      });
      that.queryGoodsList(currId);
    });
  },

  /**
   * 根据商品类型查询商品
   */
  queryGoodsList: function (currType){
    var that = this;
    var pageNum = that.data.pageNum;
    wx.showNavigationBarLoading();
    console.log(that.data.shopId);
    app.request("WxGoodsController", "queryGoodsList", {
      cat_id: currType,
      user_id: app.d.userId,
      shop_id: that.data.shopId,
      currentPage: pageNum,
    }, function (data) {
      var total = data.pro.pagination.total;
      if (that.data.status == 1) {
        that.setData({
          goodsList: [],
        })
      }
      var shoplist = that.data.goodsList;
      if (data.pro.list != null && data.pro.list.length > 0) {
        pageNum += 1;
        for (var i = 0; i < data.pro.list.length; i++) {
          shoplist.push(data.pro.list[i]);
        }
      }
      that.setData({
        goodsList: shoplist,
        pageNum: pageNum,
        loadLength: 5,
      });
      wx.hideNavigationBarLoading();
      if (shoplist.length == total) {
        that.setData({
          isNoGoods: true,
        });
      } else {
        that.setData({
          isNoGoods: false,
        });
      }
    });
  },

  /**
   * 点击分类item查询事件
   */
  tapType: function (e) {
    var that = this;
    const currType = e.currentTarget.dataset.typeId;
    that.setData({
      currType: currType,
      pageNum: 1,
      status: 1,
    });
    that.queryGoodsList(currType);
  },

  onRefresh: function () {
    var that = this;
    if (that.data.loadLength > 0) {
      that.setData({
        pageNum: 1,
        status: 1,
        loadLength: -5,
      });
      that.queryGoodsList(that.data.currType);
    }
  },

  onLoadmore: function () {
    var that = this;
    if (that.data.loadLength > 0) {
      that.setData({
        status: 2,
        loadLength: -5,
      });
      that.queryGoodsList(that.data.currType);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.queryTypesList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      var data = new Date();
      var shared = data.getTime()+"";
      var shareId = shared.substring(8, shared.length) + app.d.userId;
      return {
          title: '大肚商城',
          path: '/pages/index/index?duid=' + app.d.userId + "&shareId=" + shareId,
          success: function(res) {
              app.request("DistributorService", "shareLog", {
                  uid: app.d.userId,
                  shareId: shareId,
              }, function(data){
                  // 分享成功
                  console.log(res);
              })
          },
          fail: function(res) {
              // 分享失败
              console.log(res);
          }
      }
  }
})